﻿[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace TradePublish
{
    using TradePublish.Common;
    using log4net;
    using System;
    using System.Configuration;
    using System.IO;
    using System.Reflection;
    using System.Threading;
    using System.Windows.Forms;
    using SysThread.Grasp;
    using TradePublish.SysThread.Grasp;
    using TradePublish.SysThread.Sys;

    internal static class Program
    {
        [STAThread]
        private static void Main()
        {
            ILog logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
           
            logger.Info("系统启动");
            AppSettingsReader reader = new AppSettingsReader();
            Global.isServer = Convert.ToBoolean(reader.GetValue("isServer", typeof(bool)));
            Global.isSearchEmail = Convert.ToBoolean(reader.GetValue("isSearchEmail", typeof(bool)));
            Global.clientNo = Convert.ToInt32(reader.GetValue("clientNo", typeof(int)));
            Global.IPDBLinkStrSQL = reader.GetValue("IPDBLinkStrSQL", typeof(string)).ToString();
            Global.serverNameInlan = reader.GetValue("serverNameInlan", typeof(string)).ToString();
            if (!Directory.Exists(Application.StartupPath + "/tmpGif"))
            {
                Directory.CreateDirectory(Application.StartupPath + "/tmpGif");
            }
            new TradePublish.SysThread.Grasp.MonitorThread().start();
            logger.Info("线程MonitorThread[监测系统内存]启动.");
            if (Global.isServer)
            {
                #region 只启动一个
                bool flag;
                Mutex mutex = new Mutex(true, "TradePublish", out flag);
                if (!flag)
                {
                   return;
                }
               // mutex.ReleaseMutex();
                #endregion

                Global.TMImageBasePath = reader.GetValue("TMImageBasePath", typeof(string)).ToString();
                Global.DBLinkStrSQL = reader.GetValue("DBLinkStrSQL", typeof(string)).ToString();

                new SysThread.Grasp.DetectThread().start();
                logger.Info("线程DetectThread[检测最新公告]启动.");

                new TradePublish.SysThread.Grasp.AnalyGifThread().start();
                logger.Info("线程AnalyGifThread[分析gif图片]启动.");


                new SearchEmailTaskServerThread().start();
                logger.Info("线程SearchEmailTaskServerThread[监听节点，分配采集邮件任务线程]启动.");

                new GraspTMImageThread().start();
                logger.Info("线程graspTMImageThread[采集Trade_publish商标图片线程]启动.");

                new GraspDissentTMImageThread().start();
                logger.Info("线程graspDissentTMImageThread[采集dissent_trade商标图片线程]启动.");
            }
            else
            {
                Global.serverIpInlan = reader.GetValue("serverIpInlan", typeof(string)).ToString();
                string newValue = new IPListDao().getIp();
                Global.DBLinkStrSQL = reader.GetValue("DBLinkStrSQL", typeof(string)).ToString().Replace("localhost", newValue);
                new IpThread().start();
                logger.Info("线程IpThread[获得数据库IP]启动.");

                if (!Global.isSearchEmail)
                {
                    new ClientTaskThread().start();
                    logger.Info("线程ClientTaskThread[客户端反查管理类线程]启动.");
                }
                else
                {
                    new SearchEmailTaskClientThread().start();
                    logger.Info("线程SearchEmailTaskClientThread[采集Email的客户器端线程]启动.");
                }

            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form2());
        }
    }
}

